package com.onluy.framework.system.filter;
  
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; 
import org.apache.log4j.Logger;  
import com.alibaba.fastjson.JSON;
import com.onluy.framework.data.util.InParam;
import com.onluy.framework.system.common.ParameterRequestWrapper;
import com.onluy.framework.tools.DesUtils; 

public class ParamDecodeFilter implements Filter {
	Logger log = Logger.getLogger(ParamDecodeFilter.class);  
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		String  contenttype =httpServletRequest.getContentType();
		log.info("请求  URL:"+httpServletRequest.getRequestURI());
		log.info("请求  URL参数:"+httpServletRequest.getQueryString());
		log.info("请求方式:"+httpServletRequest.getMethod());
		log.info("请求数据类型:"+contenttype);
		log.info("请求数据CharacterEncoding:"+request.getCharacterEncoding()); 
		contenttype=contenttype==null?"":contenttype;
		
		Map<String, String[]> params = params=new HashMap<String, String[]>(); 
		params.putAll(request.getParameterMap()); 
		  
		  
		//GET 请求
		if("GET".equals(httpServletRequest.getMethod())){
			
		}
 
		if("POST".equals(httpServletRequest.getMethod()) && ! contenttype.toLowerCase().contains( "multipart/form-data")   ){	 			 
			ParameterRequestWrapper  hostrequest = new ParameterRequestWrapper(httpServletRequest, null); 
			StringBuffer json = new StringBuffer();
			   String line = null;
			   try {
				   BufferedReader reader = hostrequest.getReader();
				   while((line = reader.readLine()) != null) {
					   		json.append(line); 
				   } 
			   }
			   catch(Exception e) {
			   }
			 log.info("====================Reader.json:"+json);   
			 
			if(json.length()>0){
			  String[] paralist=json.toString().split("&");
			  for (String parastr : paralist) {
				  String[] keyvalue=parastr.split("=");
				  if(keyvalue.length!=2)continue;
				  params.put(keyvalue[0], new String[]{keyvalue[1]}); 
			  } 
			}  
		} 
 
	    
		//解密
		if(params.containsKey("reqParam")){
			String[] reqParam=params.get( "reqParam");
			if(reqParam.length>0) { 
				String param=reqParam[0]; 
				Map<String, Object > newParam =new HashMap<String, Object >();
				String newParamStr = DesUtils.dec2(param);
				log.info("====================解密后参数 :"+newParamStr); 
				newParam = JSON.parseObject(newParamStr, InParam.class);   
				for (Map.Entry<String, Object> entry : newParam.entrySet()) { 
					//log.info("====================Key = " + entry.getKey() + ", Value = " + entry.getValue());  
					params.put(entry.getKey() , new String[]{ entry.getValue().toString() });  
				}  
			}
		}

	    
		if(contenttype.toLowerCase().contains( "multipart/form-data") ) {
			log.info("====================multipart/form-data 数据类型不解密！"); 
		}
		else {
			log.info("====================解密后重新生成 请求参数:"+params.toString()); 
			request = new ParameterRequestWrapper(httpServletRequest, params);
			
		}
		
		chain.doFilter(request, response);
	}

	public void destroy() {
	}

	public void init(FilterConfig arg0) throws ServletException {
	}
}